在高性能硬體中,速度就是生命。想像一顆 GPU 正在執行 Z-緩衝:它必須每秒排序數百萬個深度值,以判斷哪個像素在前方。為了達成此目標,工程師仰賴 無符號數字比較器,一種簡化電路,可從最左位(MSB)到最右位(LSB)逐位處理,且無任何認知負擔。
二補數的失敗
標準的二補數無法通過這種「笨拙硬體」的測試。因為負數的符號位為 1,正數則為 0,所以像 -1(111...)在位元層面上看起來比 +1(001...)還大。這造成了 不連續性,迫使硬體使用複雜且較慢的條件邏輯來判斷數值大小。
單調性解決方案
為恢復效率,我們採用 偏移編碼 (偏置表示法)。透過將範圍平移,使最小可能的值對應至 000... ,而最大值則對應至 111...,確保位元模式能唯一識別一個數值,使其 字典序 完全符合其數值順序。
此特性讓『笨拙』的硬體比較器能立即處理『聰明』的浮點資料。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the primary architectural advantage of using Excess Encoding for exponents?
It allows the use of simple, fast unsigned comparators.
It increases the total range of representable numbers.
It eliminates the need for a sign bit in the mantissa.
It prevents overflow in integer addition.
✅ Correct!
Correct! By mapping the range monotonically, hardware can treat biased exponents as simple unsigned integers for comparison.❌ Incorrect
Excess encoding specifically targets the logic complexity of comparison, not the numerical range or mantissa properties.QUESTION 2
In a 3-bit Two's Complement system, which bit pattern appears 'larger' to an unsigned comparator: -1 or 0?
-1 (pattern 111)
0 (pattern 000)
They appear equal.
Neither, the comparator crashes.
✅ Correct!
Exactly. 111 is binary 7, while 000 is 0. An unsigned comparator sees -1 as 'larger' than 0.❌ Incorrect
Remember, an unsigned comparator doesn't know about sign bits. It just compares 111 vs 000.QUESTION 3
What property is defined by 'as the bit pattern increases, the represented decimal value also increases'?
Associativity
Monotonicity
Normalized Representation
Truncation
✅ Correct!
Monotonicity is the essential property that allows hardware sorting to work on biased data types.❌ Incorrect
Normalized representation refers to the leading 1 in the mantissa, not the sorting order.QUESTION 4
Why is the sign-bit discontinuity a problem for GPUs?
It forces sorting to happen on the CPU instead.
It necessitates complex, multi-stage conditional logic for every comparison.
It makes depth values (Z-buffer) impossible to store.
It causes bit-flip errors in the LSB.
✅ Correct!
Complex logic takes more transistors and more clock cycles. GPUs require massive throughput, so simple comparators are preferred.❌ Incorrect
The issue is speed and circuit complexity, not storage impossibility.QUESTION 5
If we use Excess-127 for an 8-bit exponent, what bit pattern represents the value 0?
00000000
01111111
10000000
11111111
✅ Correct!
In Excess-127, the pattern 127 (01111111) represents 0. Patterns below it are negative, patterns above are positive.❌ Incorrect
00000000 would represent -127 in an Excess-127 scheme.Hardware Architecture Case Study: Z-Buffer Optimization
Analyzing comparison logic in massively parallel units.
A hardware architect is designing a depth-sorting unit for a mobile GPU. To minimize power, they want to avoid 'Signed Integer Arithmetic Units' and instead use only 16-bit Unsigned Comparators to determine which of two depth values (Z) is closer to the camera.
Q
1. If depth values are stored in Two's Complement, why will a simple unsigned comparator fail to determine which pixel is 'behind' (-2) vs 'in front' (1)?
Solution:
In 16-bit Two's Complement, -2 is represented as 1111111111111110. An unsigned comparator interprets this bit pattern as 65,534. Since 65,534 > 1, the hardware would incorrectly conclude that -2 is much larger than 1, failing the depth test.
In 16-bit Two's Complement, -2 is represented as 1111111111111110. An unsigned comparator interprets this bit pattern as 65,534. Since 65,534 > 1, the hardware would incorrectly conclude that -2 is much larger than 1, failing the depth test.
Q
2. How does applying an Excess-K bias transform this into a 'hardware-friendly' sorting problem?
Solution:
By adding a bias (K), the entire range of possible depth values is shifted into the positive domain. This ensures that the most negative value becomes 00...0 and the most positive becomes 11...1. The bitwise order now perfectly matches the numerical order, allowing the unsigned comparator to work correctly without modification.
By adding a bias (K), the entire range of possible depth values is shifted into the positive domain. This ensures that the most negative value becomes 00...0 and the most positive becomes 11...1. The bitwise order now perfectly matches the numerical order, allowing the unsigned comparator to work correctly without modification.